﻿using System;

namespace Framework.Library.Core.Utils
{
    public class ConvertUtility
    {
        public static bool ToBoolean(object obj)
        {
            return ToBoolean(obj, false);
        }

        public static bool ToBoolean(object obj, bool defaultValue)
        {
            bool retVal;
            try
            {
                retVal = Convert.ToBoolean(obj);
            }
            catch
            {
                retVal = defaultValue;
            }
            return retVal;
        }
        
        public static DateTime ToDateTime(object obj)
        {
           return ToDateTime(obj, DateTime.MinValue);
        }

        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            DateTime retVal;
            try
            {
                retVal = Convert.ToDateTime(obj);
            }
            catch
            {
                retVal = defaultValue;
            }
            return retVal;
        }

        public static double ToDouble(object obj)
        {
            return ToDouble(obj, double.MinValue);
        }

        public static double ToDouble(object obj, double defaultValue)
        {
            double retVal;
            try
            {
                retVal = Convert.ToDouble(obj);
            }
            catch
            {
                retVal = defaultValue;
            }
            return retVal;
        }

        public static int ToInt32(object obj)
        {
            return ToInt32(obj, int.MinValue);
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            int retVal;
            int.TryParse(obj.ToString(), out retVal);
            return retVal > 0 ? retVal : defaultValue;
        }

        public static string ToString(object obj)
        {
            return ToString(obj, string.Empty);
        }

        public static string ToString(object obj, string defaultValue)
        {
            string retVal;
            try
            {
                retVal = Convert.ToString(obj);
            }
            catch
            {
                retVal = defaultValue;
            }
            return retVal;
        }

        public static string ToSubString(string obj, int length)
        {
            if (string.IsNullOrEmpty(obj))
                return string.Empty;
            if (obj.Length <= length)
                return obj;

            string mSource = obj;
            int nLength = length;

            int m = mSource.Length;
            while (nLength > 0 && mSource[nLength].ToString() != " ")
            {
                nLength--;
            }
            mSource = mSource.Substring(0, nLength);
            return mSource + "...";
        }

        public static DateTime ToDateTime(long timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddMilliseconds(timestamp);
        }

        public static long ToUnixTime(DateTime date)
        {
            return (date.ToUniversalTime().Ticks - 621355968000000000) / 10000;
        }

    }
}